package com.maikec.configure;
/**
 * 邢帅教育
 * <p>
 * 本源代码由数据中心及其作者共同所有，未经版权持有者的事先书面授权，
 * 不得使用、复制、修改、合并、发布、分发和/或销售本源代码的副本。
 *
 * @copyright Copyright (c) 2018. [图片]xsteach.com all rights reserved.
 */

import com.maikec.utils.jwt.JwtFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * JWT配置
 * @author maikec
 * @date 2018/7/24
 */
@Configuration
public class JwtConfig {
    private static String secretKey;
    private static int expiration;
    private static String issuer;
    private static String subject;
    private static String[] permitUrl;
    private static boolean isProd;


    @Value("${jwt.secretKey}")
    public void setSecretKey(String secretKey) {
        JwtConfig.secretKey = secretKey;
    }

    @Value("${jwt.expiration}")
    public void setExpiration(int expiration) {
        JwtConfig.expiration = expiration;
    }

    @Value("${jwt.issuer}")
    public void setIssuer(String issuer) {
        JwtConfig.issuer = issuer;
    }

    @Value("${jwt.subject}")
    public void setSubject(String subject) {
        JwtConfig.subject = subject;
    }
    @Value("#{'${jwt.permitUri}'.split(',')}")
    public void setPermitUrl(String[] permitUrl) { JwtConfig.permitUrl = permitUrl;}
    @Value("${jwt.isProd}")
    public void setIsProd(boolean isProd) {JwtConfig.isProd = isProd;}
    public static String getSecretKey() {
        return secretKey;
    }

    public static int getExpiration() {
        return expiration;
    }

    public static String getIssuer() {
        return issuer;
    }

    public static String getSubject() {
        return subject;
    }
    public static String[] getPermitUrl() {
        return permitUrl;
    }
    public static boolean getIsProd() {return  isProd;}

    @Bean
    public FilterRegistrationBean authFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setName("jwtFilter");
        JwtFilter jwtFilter = new JwtFilter();
        registrationBean.setFilter(jwtFilter);
        registrationBean.setOrder(1);
        List<String> urlList = new ArrayList<String>();
        urlList.add("/*");
        registrationBean.setUrlPatterns(urlList);
        registrationBean.setAsyncSupported(true);
        return registrationBean;
    }

}
